import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { NameValueDto, NavigationServiceProxy,MenuItemDto,NewSortNumberInput } from '@shared/service-proxies/service-proxies';
import { map as _map } from 'lodash-es';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { LazyLoadEvent } from 'primeng/api';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'editSortNumModal',
    templateUrl: './edit-sortnum-modal.component.html'
})
export class EditSortComponent extends AppComponentBase {
    @Output() menuUpdated: EventEmitter<MenuItemDto> = new EventEmitter<MenuItemDto>();
    @ViewChild('editmodal', {static: true}) modal: ModalDirective;

    saving = false;

    _menuItemDto:MenuItemDto = new MenuItemDto();

    constructor(
        injector: Injector,
        private _navigationService: NavigationServiceProxy
    ) {
        super(injector);
    }

    show(om: MenuItemDto): void {
        this._menuItemDto = om;
        this.modal.show();
    }

    save(): void {
        const Input = new NewSortNumberInput({
            id: this._menuItemDto.id,
            newNumber: this._menuItemDto.order
        });
        this.saving = true;
        this._navigationService
            .changeSortNum(Input)
            .pipe(finalize(() => this.saving = false))
            .subscribe(result => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.menuUpdated.emit(result);
            });
    }

    close(): void {
        this.modal.hide();
    }
}
